package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.FriendRelation;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.ChatMapper;
import cn.wolfcode.trip.base.mapper.FriendRelationMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private FriendRelationMapper friendRelationMapper;
    @Autowired
    private ChatMapper chatMapper;

    public void update(User u) {
        userMapper.updateByPrimaryKey(u);
    }

    //进行好友关系的的存储
    public void addFriend(FriendRelation friendRelation) {
        FriendRelation relation=friendRelationMapper.selectByRelation(friendRelation);
        if (relation!=null){
            if (relation.getState()==relation.STATE_AWAIT){
                throw new RuntimeException("等待对方验证");
            }
            throw new RuntimeException("已经是好友了");
        }
        if (friendRelation.getUser().getId()==friendRelation.getFriend().getId()){
            throw new RuntimeException("自己不能添加自己");
        }
        friendRelation.setCreateTime(new Date());
        friendRelationMapper.insertUserRelation(friendRelation);
        friendRelation.setState(relation.STATE_APPLY);
        friendRelationMapper.insertFriendRelation(friendRelation);
    }

    //查询当前登录用户的好友
    public PageInfo listForFriend(UserQueryObject qo) {
        qo.setState(FriendRelation.STATE_SUCCESS);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectFriendForUserId(qo);
        return new PageInfo<>(list);
    }

    //查询当前登录用户申请好友
    public PageInfo listApplyFriend(UserQueryObject qo) {
        qo.setState(FriendRelation.STATE_APPLY);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectlistApplyFriendById(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void acceptRelation(Long id, Long userId) {
        friendRelationMapper.updateFriendRelation(id,userId);
    }
    //游记点赞或者取消
    public String likeFunction(Long travelId, Long userId) {
        //设置当前页面返回值
        String msg;
        //查询当前用户和当前游记在数据库是否有数据
        Map<String,Object> map=userMapper.selectLikeRelation(travelId,userId);
        //当有数据时删除当前数据并设置返回值为取消
        if (map != null) {
            userMapper.deleteTravelRelation(travelId,userId);
            msg="取消点赞";
        }else{
                //直接判断为可以点赞
                userMapper.insertLikeRelation(travelId,userId,new Date());
                msg="点赞成功";

        }
        return msg;
    }

    @Override
    public void likeRelation(Long travelId, Long userId) {
        Map<String,Object> map=userMapper.selectLikeRelation(travelId,userId);
        if (map != null) {
            throw new RuntimeException("已经收藏");
        }
    }

    //游记收藏或者取消
    public String enshrineFunction(Long travelId, Long userId) {
        //设置当前页面返回值
        String msg;
        //查询当前用户和当前游记在数据库是否有数据
        Map<String,Object> map=userMapper.selectEnshrineRelation(travelId,userId);
        //当有数据时删除当前数据并设置返回值为取消
        if (map != null) {
            userMapper.deleteTravelRelation(travelId,userId);
            msg="取消收藏";
        }else{
            //没数据时进行新增数据
            Integer i = travelMapper.selectEnshrineRelation(travelId, userId);
            if (i<=0){
                //直接判断为可以点赞
                userMapper.insertTravelRelation(travelId,userId,new Date());
                msg="收藏成功";
            }else{
                msg="不能收藏自己";
            }
        }
        return msg;
    }

    @Override
    public void enshrineRelation(Long travelId, Long userId) {
        Map<String,Object> map=userMapper.selectEnshrineRelation(travelId,userId);
        if (map != null) {
            throw new RuntimeException("已经收藏");
        }
    }

    @Override
    public User UserMessage(Long id) {

      return   userMapper.selectByUserId(id);

    }

    @Override
    public String attention(Long attentionId, Long userId) {
        String msg;
        Map<String,Object> map=userMapper.selectAttentionRelation(attentionId,userId);
        if (map != null) {
            userMapper.deleteAttentionRelation(attentionId,userId);
            msg="取消关注";
        }else{
            if (attentionId!=userId){
                userMapper.insertAttentionRelation(attentionId,userId);
                msg="关注成功";
            }else{
                msg="不能关注自己";
            }
        }
        return msg;

    }

    @Override
    public void attentionState(Long attentionId, Long userId) {
        Map<String,Object> map=userMapper.selectAttentionRelation(attentionId,userId);
        if (map==null){
            throw new RuntimeException("1");
        }

    }

    @Override
    public Map<String, Object> attentionNumber(Long id) {
        return userMapper.selectAttentionNumber(id);
    }

    @Override
    public Map<String, Object> fansNumber(Long id) {
        return userMapper.selectFansNumber(id);
    }

    @Override
    public Map<String, Object> FriendsNoticeNumber(Long id) {

        return userMapper.FriendsNoticeNumber(id);
    }

    @Override
    public Map<String, Object> commenteNumber(Long id) {
        return userMapper.commenteNumber(id);
    }



    public PageInfo<User> list(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public User login(String email, String password) {
        User user = userMapper.selectByInfo(email, password);
        if (user == null) { //登录失败
            throw new RuntimeException("账号和密码不匹配");
        }
        return user;
    }

    public void regist(User u) {
        //检查用户是否存在
        String email = userMapper.selectByEmail(u.getEmail());
        if (email != null) {
            //当前用户已经注册
            throw new RuntimeException("该用户已经注册");
        }

        //先填充默认数据
        u.setPlace("亚洲");
        u.setGender(User.GENDER_NONE);
        u.setSign("还没有签名喔");
        u.setHeadImgUrl("/upload/head.jpg");
        u.setCoverImgUrl("/upload/bg.jpeg");
        //在保存到数据库
        userMapper.insert(u);
    }
}
